//
//  WmcMapManager.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/26.
//

import Foundation
import CoreLocation
import MapKit

struct WmcMapManager {
    // Internal

    enum MapType {
        // 苹果
        case appleMap
        // 百度
        case baiduMap
        // 高德
        case gaodeMap
        // 腾讯
        case qqMap

        var uri: String {
            switch self {
            case .appleMap:
                return "http://maps.apple.com/"
            case .baiduMap:
                return "baidumap://"
            case .gaodeMap:
                return "iosamap://"
            case .qqMap:
                return "qqmap://"
            }
        }

        var name: String {
            switch self {
            case .appleMap:
                return "苹果地图"
            case .baiduMap:
                return "百度地图"
            case .gaodeMap:
                return "高德地图"
            case .qqMap:
                return "腾讯地图"
            }
        }
    }

    static func canOpen(type: MapType) -> Bool {
        guard let url = URL(string: type.uri),
              UIApplication.shared.canOpenURL(url) else
        {
            return false
        }
        return true
    }

    static func canOpenMaps() -> [MapType] {
        var list: [MapType] = []
        if Self.canOpen(type: .appleMap) {
            list.append(.appleMap)
        }
        if Self.canOpen(type: .baiduMap) {
            list.append(.baiduMap)
        }
        if Self.canOpen(type: .gaodeMap) {
            list.append(.gaodeMap)
        }
        if Self.canOpen(type: .qqMap) {
            list.append(.qqMap)
        }
        return list
    }

    static func openMap(latitude: Double, longitute: Double, name: String, mapType: MapType) {
        if mapType == .appleMap {
            let loc = CLLocationCoordinate2DMake(latitude, longitute)
            let currentLocation = MKMapItem.forCurrentLocation()
            let toLocation = MKMapItem(placemark: MKPlacemark(coordinate: loc, addressDictionary: nil))
            toLocation.name = name
            MKMapItem.openMaps(with: [currentLocation, toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: NSNumber(value: true)])
        } else {
            let url = Self.convertToMapUrl(latitude: latitude, longitute: longitute, name: name, mapType: mapType)
            Self.openURL(urlString: url)
        }
    }

    static func convertToMapUrl(latitude: Double, longitute: Double, name: String, mapType: MapType) -> String {
        var urlString = ""
        switch mapType {
        case .appleMap:
            urlString = ""
        case .baiduMap:
            // baidumap://map/direction?origin=中关村&destination=五道口&mode=driving&region=北京&src=ios.baidu.openAPIdemo
            urlString = "baidumap://map/direction?origin={{我的位置}}&destination=latlng:\(latitude),\(longitute)|name=\(name)&mode=driving&coord_type=gcj02"
        case .gaodeMap:
            urlString = "iosamap://path?sourceApplication=applicationName&sid=BGVIS1&did=BGVIS2&dlat=\(latitude)&dlon=\(longitute)&dname=\(name)&dev=0&m=0&t=0"
        case .qqMap:
            urlString = "qqmap://map/routeplan?from=我的位置&type=drive&tocoord=\(latitude),\(longitute)&to=\(name)&coord_type=1&policy=0"
        }

        return urlString
    }

    // Private

    private static func openURL(urlString: String) {
        let urlStr = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? ""

        guard let url = URL(string: urlStr) else { return }
        
        if #available(iOS 10, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: { (success) in

            })
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}
